package icraf.carbon.gui;

import icraf.carbon.core.LandCover;
import icraf.carbon.core.LandCoverList;
import icraf.carbon.core.Project;

import javax.swing.ListModel;

public class EligibilityTableModel extends MatrixTableModel {

	private static final long serialVersionUID = 8968868974903218229L;

	private boolean editable = true;
	private LCRowHeaderModel lcRowHeaderModel = new LCRowHeaderModel(this);
	private Project project;
	


	public EligibilityTableModel() {
		super();
	}
	

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return Boolean.class;
	}

	@Override
	public int getColumnCount() {
		if (project != null) {
			return project.getLandCoverList().size();
		}
		return 0;
	}

	public String getColumnName(int col) {
		if (project == null)
			return "";
		return project.getLandCoverList().get(col).getLabel();
	}


	@Override
	public int getRowCount() {
		return getColumnCount();
	}

	public ListModel<Object> getRowHeaderModel() {
		return lcRowHeaderModel;
	}

	@Override
	public Object getValueAt(int row, int column) {
		if (project == null)
			return null;
		LandCoverList l = project.getLandCoverList();
		LandCover lcRow = l.get(row);
		LandCover lcCol = l.get(column);
		return project.isEligible(lcRow, lcCol);
	}

	public Project getProject() {
		return project;
	}

	public boolean isCellEditable(int row, int col) {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public void setProject(Project z) {
		this.project = z;
		lcRowHeaderModel.setProject(z);
		fireTableStructureChanged();
	}


	public void setValueAt(Object value, int row, int col) {
			boolean d = ((Boolean) value).booleanValue();
		
		LandCoverList l = project.getLandCoverList();
		LandCover lcRow = l.get(row);
		LandCover lcCol = l.get(col);
		project.setEligibility(d, lcRow, lcCol);
		fireTableCellUpdated(row, col);
	}

	public String getInfo(int row, int column) {
		LandCoverList l = project.getLandCoverList();
		LandCover lc1 = l.get(row);
		LandCover lc2 = l.get(column);
		String e =project.isEligible(lc1, lc2)?"": "Not";
		String s = "The transition from:\r\n"+lc1.getLabel()+"\r\nto:\r\n"+lc2.getLabel()+"\r\nis "+e+" Eligible";
		return s;
	}


}
